if len(root.childNodes) == 2 and root.childNodes.length == 2 and root.childNodes[0] is nelem and root.childNodes.item(0) is nelem and root.childNodes[1] is elem and root.childNodes.item(1) is elem and root.firstChild is nelem and root.lastChild is elem:
pass
confirm(root.toxml() == '<doc><element/><foo/></doc>', 'testInsertBefore -- node properly placed in tree')
nelem = dom.createElement('element')
root.insertBefore(nelem, None)
if len(root.childNodes) == 3 and root.childNodes.length == 3 and root.childNodes[1] is elem and root.childNodes.item(1) is elem and root.childNodes[2] is nelem and root.childNodes.item(2) is nelem and root.lastChild is nelem and nelem.previousSibling is elem:
pass
confirm(root.toxml() == '<doc><element/><foo/><element/></doc>', 'testInsertBefore -- node properly placed in tree')
nelem2 = dom.createElement('bar')
root.insertBefore(nelem2, nelem)
if len(root.childNodes) == 4 and root.childNodes.length == 4 and root.childNodes[2] is nelem2 and root.childNodes.item(2) is nelem2 and root.childNodes[3] is nelem and root.childNodes.item(3) is nelem and nelem2.nextSibling is nelem and nelem.previousSibling is nelem2:
pass
confirm(root.toxml() == '<doc><element/><foo/><bar/><element/></doc>', 'testInsertBefore -- node properly placed in tree')
if len(el.attributes) == 1 and el.attributes['spam'].value == 'bam' and el.attributes['spam'].nodeValue == 'bam' and el.getAttribute('spam') == 'bam':
pass
confirm(el.getAttributeNode('spam').isId)
el.attributes['spam'] = 'ham'
if len(el.attributes) == 1 and el.attributes['spam'].value == 'ham' and el.attributes['spam'].nodeValue == 'ham' and el.getAttribute('spam') == 'ham':
pass
confirm(el.attributes['spam'].isId)
el.setAttribute('spam2', 'bam')
if len(el.attributes) == 2 and el.attributes['spam'].value == 'ham' and el.attributes['spam'].nodeValue == 'ham' and el.getAttribute('spam') == 'ham' and el.attributes['spam2'].value == 'bam' and el.attributes['spam2'].nodeValue == 'bam':
pass
confirm(el.getAttribute('spam2') == 'bam')
el.attributes['spam2'] = 'bam2'
if len(el.attributes) == 2 and el.attributes['spam'].value == 'ham' and el.attributes['spam'].nodeValue == 'ham' and el.getAttribute('spam') == 'ham' and el.attributes['spam2'].value == 'bam2' and el.attributes['spam2'].nodeValue == 'bam2':
pass
confirm(el.getAttribute('spam2') == 'bam2')
dom.unlink()
def testGetAttrList():
pass
def testGetAttrValues():
pass
def testGetAttrLength():
pass
def testGetAttribute():
pass
def testGetAttributeNS():
pass
def testGetAttributeNode():
pass
def testGetElementsByTagNameNS():
d = "<foo xmlns:minidom='http://pyxml.sf.net/minidom'>\n <minidom:myelem/>\n </foo>"
if len(elems) == 1 and elems[0].namespaceURI == 'http://pyxml.sf.net/minidom' and elems[0].localName == 'myelem' and elems[0].prefix == 'minidom' and elems[0].tagName == 'minidom:myelem':
el = dom.appendChild(dom.createElementNS(u'http://www.slashdot.org', u'slash:abc'))
string1 = repr(el)
string2 = str(el)
confirm(string1 == string2)
confirm(string1.find('slash:abc') != -1)
dom.unlink()
def testAttributeRepr():
dom = Document()
el = dom.appendChild(dom.createElement(u'abc'))
node = el.setAttribute('abc', 'def')
confirm(str(node) == repr(node))
dom.unlink()
def testTextNodeRepr():
pass
def testWriteXML():
str = '<?xml version="1.0" ?><a b="c"/>'
dom = parseString(str)
domstr = dom.toxml()
dom.unlink()
confirm(str == domstr)
def testAltNewline():
str = '<?xml version="1.0" ?>\n<a b="c"/>\n'
dom = parseString(str)
domstr = dom.toprettyxml(newl = '\r\n')
dom.unlink()
confirm(domstr == str.replace('\n', '\r\n'))
def testProcessingInstruction():
dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
pi = dom.documentElement.firstChild
if pi.target == 'mypi' and pi.data == 'data \t\n ' and pi.nodeName == 'mypi' and pi.nodeType == Node.PROCESSING_INSTRUCTION_NODE and pi.attributes is None and not pi.hasChildNodes() and len(pi.childNodes) == 0 and pi.firstChild is None and pi.lastChild is None and pi.localName is None:
if clone is not None and clone.nodeName == doctype.nodeName and clone.name == doctype.name and clone.publicId == doctype.publicId and clone.systemId == doctype.systemId and len(clone.entities) == len(doctype.entities) and clone.entities.item(len(clone.entities)) is None and len(clone.notations) == len(doctype.notations) and clone.notations.item(len(clone.notations)) is None:
pass
confirm(len(clone.childNodes) == 0)
for i in range(len(doctype.entities)):
se = doctype.entities.item(i)
ce = clone.entities.item(i)
if not se.isSameNode(ce) and not ce.isSameNode(se) and ce.nodeName == se.nodeName and ce.notationName == se.notationName and ce.publicId == se.publicId and ce.systemId == se.systemId and ce.encoding == se.encoding and ce.actualEncoding == se.actualEncoding:
pass
confirm(ce.version == se.version)
for i in range(len(doctype.notations)):
sn = doctype.notations.item(i)
cn = clone.notations.item(i)
if not sn.isSameNode(cn) and not cn.isSameNode(sn) and cn.nodeName == sn.nodeName and cn.publicId == sn.publicId:
pass
confirm(cn.systemId == sn.systemId)
def testCloneDocumentTypeDeepNotOk():
doc = create_doc_with_doctype()
clone = doc.doctype.cloneNode(1)
confirm(clone is None, 'testCloneDocumentTypeDeepNotOk')
def testCloneDocumentTypeShallowOk():
doctype = create_nonempty_doctype()
clone = doctype.cloneNode(0)
if clone is not None and clone.nodeName == doctype.nodeName and clone.name == doctype.name and clone.publicId == doctype.publicId and clone.systemId == doctype.systemId and len(clone.entities) == 0 and clone.entities.item(0) is None and len(clone.notations) == 0 and clone.notations.item(0) is None:
pass
confirm(len(clone.childNodes) == 0)
def testCloneDocumentTypeShallowNotOk():
doc = create_doc_with_doctype()
clone = doc.doctype.cloneNode(0)
confirm(clone is None, 'testCloneDocumentTypeShallowNotOk')
def check_import_document(deep, testName):
doc1 = parseString('<doc/>')
doc2 = parseString('<doc/>')
try:
doc1.importNode(doc2, deep)
except xml.dom.NotSupportedErr:
pass
raise Exception(testName + ': expected NotSupportedErr when importing a document')
if root.parentNode is doc and elm1.parentNode is root and elm2a.parentNode is elm1 and elm2b.parentNode is elm1:
pass
confirm(elm3.parentNode is elm2b, 'testParents')
doc.unlink()
def testNodeListItem():
doc = parseString('<doc><e/><e/></doc>')
children = doc.childNodes
docelem = children[0]
if children[0] is children.item(0) and children.item(1) is None and docelem.childNodes.item(0) is docelem.childNodes[0] and docelem.childNodes.item(1) is docelem.childNodes[1]:
pass
confirm(docelem.childNodes.item(0).childNodes.item(0) is None, 'test NodeList.item()')
doc.unlink()
def testSAX2DOM():
pulldom = pulldom
import xml.dom
sax2dom = pulldom.SAX2DOM()
sax2dom.startDocument()
sax2dom.startElement('doc', { })
sax2dom.characters('text')
sax2dom.startElement('subelm', { })
sax2dom.characters('text')
sax2dom.endElement('subelm')
sax2dom.characters('text')
sax2dom.endElement('doc')
sax2dom.endDocument()
doc = sax2dom.document
root = doc.documentElement
(text1, elm1, text2) = root.childNodes
text3 = elm1.childNodes[0]
if text1.previousSibling is None and text1.nextSibling is elm1 and elm1.previousSibling is text1 and elm1.nextSibling is text2 and text2.previousSibling is elm1 and text2.nextSibling is None and text3.previousSibling is None:
pass
confirm(text3.nextSibling is None, 'testSAX2DOM - siblings')
if root.parentNode is doc and text1.parentNode is root and elm1.parentNode is root and text2.parentNode is root:
pass
confirm(text3.parentNode is elm1, 'testSAX2DOM - parents')
doc.unlink()
def testEncodings():
doc = parseString('<foo>€</foo>')
if doc.toxml() == u'<?xml version="1.0" ?><foo>Γé¼</foo>' and doc.toxml('utf-8') == '<?xml version="1.0" encoding="utf-8"?><foo>\xe2\x82\xac</foo>':
pass
confirm(doc.toxml('iso-8859-15') == '<?xml version="1.0" encoding="iso-8859-15"?><foo>\xa4</foo>', 'testEncodings - encoding EURO SIGN')
doc.unlink()
class UserDataHandler:
called = 0
def handle(self, operation, key, data, src, dst):
dst.setUserData(key, data + 1, self)
src.setUserData(key, None, None)
self.called = 1
def testUserData():
dom = Document()
n = dom.createElement('e')
confirm(n.getUserData('foo') is None)
n.setUserData('foo', None, None)
confirm(n.getUserData('foo') is None)
n.setUserData('foo', 12, 12)
n.setUserData('bar', 13, 13)
confirm(n.getUserData('foo') == 12)
confirm(n.getUserData('bar') == 13)
n.setUserData('foo', None, None)
confirm(n.getUserData('foo') is None)
confirm(n.getUserData('bar') == 13)
handler = UserDataHandler()
n.setUserData('bar', 12, handler)
c = n.cloneNode(1)
if handler.called and n.getUserData('bar') is None:
if attr.name == 'b' and attr.nodeName == 'b' and attr.localName is None and attr.namespaceURI == xml.dom.EMPTY_NAMESPACE and attr.prefix is None and attr.value == 'v' and elem.getAttributeNode('a') is None and elem.getAttributeNode('b').isSameNode(attr) and attrmap['b'].isSameNode(attr) and attr.ownerDocument.isSameNode(doc):
if attr.name == 'c' and attr.nodeName == 'c' and attr.localName == 'c' and attr.namespaceURI == 'http://xml.python.org/ns' and attr.prefix is None and attr.value == 'v' and elem.getAttributeNode('a') is None and elem.getAttributeNode('b') is None and elem.getAttributeNode('c').isSameNode(attr) and elem.getAttributeNodeNS('http://xml.python.org/ns', 'c').isSameNode(attr) and attrmap['c'].isSameNode(attr):
if attr.name == 'p:d' and attr.nodeName == 'p:d' and attr.localName == 'd' and attr.namespaceURI == 'http://xml.python.org/ns2' and attr.prefix == 'p' and attr.value == 'v' and elem.getAttributeNode('a') is None and elem.getAttributeNode('b') is None and elem.getAttributeNode('c') is None and elem.getAttributeNodeNS('http://xml.python.org/ns', 'c') is None and elem.getAttributeNode('p:d').isSameNode(attr) and elem.getAttributeNodeNS('http://xml.python.org/ns2', 'd').isSameNode(attr) and attrmap['p:d'].isSameNode(attr):
if attr.name == 'e' and attr.nodeName == 'e' and attr.localName is None and attr.namespaceURI == xml.dom.EMPTY_NAMESPACE and attr.prefix is None and attr.value == 'v' and elem.getAttributeNode('a') is None and elem.getAttributeNode('b') is None and elem.getAttributeNode('c') is None and elem.getAttributeNode('p:d') is None and elem.getAttributeNodeNS('http://xml.python.org/ns', 'c') is None and elem.getAttributeNode('e').isSameNode(attr):
if elem.tagName == 'p:c' and elem.nodeName == 'p:c' and elem.localName == 'c' and elem.namespaceURI == 'http://xml.python.org/ns2' and elem.prefix == 'p':